package cn.com.dealme.domain.model;

import java.util.Date;
import java.util.Set;

import cn.com.dealme.domain.model.exception.UserException;
import cn.com.dealme.domain.vo.AccountVO;
import cn.com.dealme.domain.vo.RoleVO;
import cn.com.dealme.domain.vo.UniversityVO;

public class User {

	private Long id;
	
	private AccountVO accountInfo;
	
	private UniversityVO myUniversity;
	
	private RoleVO role;
	
	private Set<Commodity> publishedCommoditySet;
	
	public User() {
	}
	
	public User(Long id) {
		this.id = id;
	}
	
	public User(AccountVO accountInfo, RoleVO role, UniversityVO myUniversity, Set<Commodity> publishedCommoditySet) {
		if(null == accountInfo) {
			throw new UserException(UserException.ACCOUNT_INFO_NULL_EXCEPTION);
		}
		if(null == role) {
			throw new UserException(UserException.ROLE_NULL_EXCEPTION);
		}
		if(null == myUniversity) {
			throw new UserException(UserException.UNIVERSITY_NULL_EXCEPTION);
		}
		this.accountInfo = accountInfo;
		this.role = role;
		this.myUniversity = myUniversity;
		this.publishedCommoditySet = publishedCommoditySet;
	}
	
	public void update(AccountVO accountInfo) {
		if(null == accountInfo) {
			throw new UserException(UserException.ACCOUNT_INFO_NULL_EXCEPTION);
		}
		this.accountInfo = accountInfo;
	}
	
	public void update(UniversityVO universityVO) {
		if(null == myUniversity) {
			throw new UserException(UserException.UNIVERSITY_NULL_EXCEPTION);
		}
		this.myUniversity = universityVO;
	}
	
	public void update(RoleVO role) {
		if(null == role) {
			throw new UserException(UserException.ROLE_NULL_EXCEPTION);
		}
		this.role = role;
	}
	
	public void ban(Date date) {
		this.accountInfo.setBanToDate(date);
	}
	
	public void publishCommodity(Commodity commodity) {
		if(null == commodity) {
			throw new UserException(UserException.PUBLISH_COMMODITY_NULL_EXCEPTION);
		}
		this.publishedCommoditySet.add(commodity);
	}
	
	public void removePublishedCommodity(Commodity commodity) {
		this.publishedCommoditySet.remove(commodity);
	}
	

	public Long getId() {
		return id;
	}

	protected void setId(Long id) {
		this.id = id;
	}

	public AccountVO getAccountInfo() {
		return accountInfo;
	}

	public UniversityVO getMyUniversity() {
		return myUniversity;
	}

	public RoleVO getRole() {
		return role;
	}

	public Set<Commodity> getPublishedCommoditySet() {
		return publishedCommoditySet;
	}

	protected void setAccountInfo(AccountVO accountInfo) {
		this.accountInfo = accountInfo;
	}

	protected void setMyUniversity(UniversityVO myUniversity) {
		this.myUniversity = myUniversity;
	}

	protected void setRole(RoleVO role) {
		this.role = role;
	}

	protected void setPublishedCommoditySet(Set<Commodity> publishedCommoditySet) {
		this.publishedCommoditySet = publishedCommoditySet;
	}
	
	@Override
	public boolean equals(Object obj) {
		if(null == obj) return false;
		if(!(obj instanceof User)) return false;
		if(this.id.equals(((User)obj).id)) {
			return true;
		}
		return false;
	}

	@Override
	public int hashCode() {
		return this.id.hashCode();
	}
}
